import json
from openai import OpenAI


client = OpenAI(api_key="sk-trzhswvhhyfdyffwseypmwgczmkkmnddwdgukrwukbknveia", base_url="https://api.siliconflow.cn/v1")
deployment_name = 'deepseek-ai/DeepSeek-V3'

def get_confusing_sentences(concepts, text):
    """
    调用大模型，询问哪些句子可能会导致疑惑，
    并要求以 JSON 格式返回，格式如下：
    {"confusing_sentences": ["句子1", "句子2", ...]}
    """
    prompt = (
        f"如果对以下概念不理解，哪些句子可能会有疑惑？请列出具体的句子，格式为：'confusing_sentences': [句子1, 句子2, ...]\n"
        f"概念列表：{', '.join(concepts)}\n"
        f"课程字幕：{text}"
    )
    response = client.chat.completions.create(
        model=deployment_name,
        messages=[{'role': 'user', 'content': prompt}],
        stream=True
    )
    result_str = ""
    for chunk in response:
        result_str += chunk.choices[0].delta.content or ""
    return result_str
    
def get_responses(user_message):
    if user_message == '':
        return ''
    if len(user_message) > 15800:
        user_message = user_message[:15800]
    response = client.chat.completions.create(
        model=deployment_name,
        messages=[
            {"role": "system",
             # "content": "You are an assistant who summarizes the course video subtitles sent by users into one paragraph."},
             "content": "你是一个助手，负责总结用户发送的课程视频字幕为一段话。(以本视频讲了为开头)"},
            {"role": "user", "content": user_message},
        ]
    )
    return response.choices[0].message.content


def get_recommendation(question,rec_list):
    template = f"""You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you don't know the answer, just say that you don't know.
Don't answer questions that are irrelevant to the question.
Question: {question}
Context: {rec_list}
Answer:
"""
    # print(template)
    response = client.chat.completions.create(
        model=deployment_name,
        messages=[
            {"role": "system",
             "content": template},
        ]
    )
    return response.choices[0].message.content


def get_recommendation_nrag(question):
    template = f"""You are an assistant for question-answering tasks.
If you don't know the answer, just say that you don't know.
Don't answer questions that are irrelevant to the question.
Question: {question}
Answer:
"""
    # print(template)
    response = client.chat.completions.create(
        model=deployment_name,
        messages=[
            {"role": "system",
             "content": template},
        ]
    )
    return response.choices[0].message.content